package com.isoft.uaaservice.bean;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginSysUserDetails implements UserDetails {
    //真正和数据表映射的登陆者实体
    SysUser sysUser;
    //权限列表
    private List<String> allowList;
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //将List<String>allowList 转换为 Collection<? extends GrantedAuthority> getAuthorities()
        List<SimpleGrantedAuthority> list = new ArrayList<>();
        if (null != allowList && allowList.size() > 0){
            //for循环遍历方法
            for (String s : allowList){
                list.add(new SimpleGrantedAuthority(s));
            }
        }
        return list;
    }
    @JsonIgnore
    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }
    @JsonIgnore
    @Override
    public String getUsername() {
        return sysUser.getUsername();
    }
    // 其他 UserDetails 方法的实现
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
